package com.shen.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shen.server.mapper.AdminRoleMapper;
import com.shen.server.pojo.AdminRole;
import com.shen.server.pojo.RespBean;
import com.shen.server.pojo.Role;
import com.shen.server.mapper.RoleMapper;
import com.shen.server.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HMF
 * @since 2022-04-10
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    private AdminRoleMapper adminRoleMapper;

    //跟新角色列表
    @Override
    @Transactional
    public RespBean updateRoles(Integer adminId, Integer[] rids) {
        //先删除为adminId的角色
        if (rids.length == 0 || rids == null){
            return RespBean.error("更新失败！");
        }
        adminRoleMapper.delete(new QueryWrapper<AdminRole>().eq("adminId",adminId));
        //在更新为adminID的角色；
        Integer result = adminRoleMapper.addAdminRole(adminId,rids);
        if (rids.length == result){
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }
}
